import Vue from 'vue';
import VueRouter from 'vue-router';
Vue.use(VueRouter);

const originalPush = VueRouter.prototype.push;
const originalReplace = VueRouter.prototype.replace;
// push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
	if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject);
	return originalPush.call(this, location).catch((err) => err);
};
// replace
VueRouter.prototype.replace = function push(location, onResolve, onReject) {
	if (onResolve || onReject) return originalReplace.call(this, location, onResolve, onReject);
	return originalReplace.call(this, location).catch((err) => err);
};
const routes = [
	{
		path: '/login',
		name: 'login',
		component: () => import('@/views/login'),
	},
	{
		path: '/register',
		name: 'register',
		component: () => import('@/views/register'),
	},
	{
		path: '/exam',
		name: 'exam',
		component: () => import('@/views/exam'),
	},
	{
		path: '/exam-pro',
		name: 'exam-pro',
		component: () => import('@/views/exam-pro'),
	},
	{
		path: '/exam-check',
		name: 'exam-check',
		component: () => import('@/views/exam-check'),
	},
	{
		path: '/comment',
		name: 'comment',
		component: () => import('@/views/comment'),
	},
];
export const adminRoutes = [
	{ path: '/', redirect: '/admin/home' },
	{
		path: '/admin',
		name: 'Admin',
		component: () => import('@/views/admin'),
		children: [
			{
				path: 'home',
				component: () => import('@/views/admin/home'),
				name: 'Home',
				meta: {
					title: '首页',
				},
			},
			{
				path: 'manage',
				component: () => import('@/views/admin/manage'),
				name: 'Manage',
				meta: {
					title: '管理列表',
				},
				children: [
					{
						path: 'user',
						component: () => import('@/views/admin/manage/user'),
						name: 'User',
						meta: {
							title: '用户管理',
						},
					},
					{
						path: 'paper',
						component: () => import('@/views/admin/manage/paper'),
						name: 'Paper',
						meta: {
							title: '试卷管理',
						},
					},
				],
			},
		],
	},
];
export const userRoutes = [
	{ path: '/', redirect: '/user/home' },
	{
		path: '/user',
		name: 'user',
		component: () => import('@/views/user'),
		children: [
			{
				path: 'home',
				component: () => import('@/views/user/home'),
				name: 'Home',
			},
			{
				path: 'papers',
				component: () => import('@/views/user/papers'),
				name: 'Papers',
			},
			{
				path: 'score',
				component: () => import('@/views/user/score'),
				name: 'Score',
			},
			{
				path: 'collect',
				component: () => import('@/views/user/collect'),
				name: 'Collect',
			},
		],
	},
];

export const createRouter = () =>
	new VueRouter({
		routes,
	});
const router = createRouter();

export default router;
